package com.eascs.ticket.configuration.controller;

import javax.inject.Inject;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.common.util.ObjectUtil;
import com.eascs.common.util.StringUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.ticket.common.entity.EntityUtil;
import com.eascs.ticket.configuration.entity.TicketStatus;
import com.eascs.ticket.configuration.entity.TicketStatusUpdate;
import com.eascs.ticket.configuration.form.GetTicketStatusViewForm;
import com.eascs.ticket.configuration.form.SaveTicketStatusForm;
import com.eascs.ticket.configuration.service.TicketStatusService;

@RestController
public class TicketStatusRestController extends BaseRestController {
	@Inject
	private TicketStatusService ticketStatusService;

	@RequestMapping("/configuration/status/getTicketStatusView")
	public HtmlRestResult getTicketStatusView(GetTicketStatusViewForm getTicketStatusViewForm) {
		if (StringUtil.isNotBlank(getTicketStatusViewForm.getId())) {
			TicketStatus ticketStatus = ticketStatusService.getTicketStatusById(getTicketStatusViewForm.getId());
			ViewContext.put("ticketStatus", ticketStatus);
		}
		return HtmlRestResult.popHtmlFragment("configuration/status/getTicketStatusView");
	}

	@RequestMapping("/configuration/status/saveTicketStatus")
	public BaseRestResult<String> saveTicketStatus(SaveTicketStatusForm saveTicketStatusForm) {
		if (StringUtil.isEmpty(saveTicketStatusForm.getId())) {
			TicketStatus ticketStatus = new TicketStatus();
			ObjectUtil.copySimpleProperties(saveTicketStatusForm, ticketStatus);
			EntityUtil.initUpdateEntity(ticketStatus);
			this.ticketStatusService.insertTicketStatus(ticketStatus);
		} else {
			TicketStatusUpdate ticketStatusUpdate = new TicketStatusUpdate();
			ObjectUtil.copySimpleProperties(saveTicketStatusForm, ticketStatusUpdate);
			EntityUtil.initUpdateEntity(ticketStatusUpdate);
			this.ticketStatusService.updateTicketStatus(ticketStatusUpdate);
		}

		return BaseRestResult.refresh();
	}

}
